# encoding: utf-8
#	id         | integer                     | not null default nextval('parcelados_id_seq'::regclass)
#	data       | date                        | 
#	valor      | numeric                     | 
#	observacao | character varying(255)      | 
#	parcelas   | integer                     | 
#	created_at | timestamp without time zone | not null
#	updated_at | timestamp without time zone | not null
# usuario_id |														 |
class Parcelado < ActiveRecord::Base
  attr_protected
  
  belongs_to :usuario
  has_many :parcelamento, :order => "ordem asc",
  	:class_name => "Parcela", :dependent => :destroy
  
  accepts_nested_attributes_for :parcelamento

  validates :usuario, :parcelas, :observacao, :data, :valor,
  	:presence => true
  
  #
  #
  def saldo_devedor
  	saldo = BigDecimal.new("0.00")
  	
  	parcelamento.each { |p| saldo += p.valor unless p.quitada? }
  	
  	return saldo
  end
  
  #
  # override do método original
  def deep_dup
  	obj = self.dup
  	
  	parcelamento.each do |p|
  		obj.parcelamento << p.dup
  	end
  	
  	return obj
  end
  
end
